package huawei.graphics.drawable;

import android.animation.ObjectAnimator;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.BitmapDrawable;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;
import huawei.hwanimation.CubicBezierInterpolator;

public class HwLoadingDrawableImpl extends BitmapDrawable implements Animatable {
    private static final int ALPHA_BASIC = 127;
    private static final int ALPHA_FULL = 255;
    private static final int ALPHA_INIT = 128;
    private static final int ANGLE = 30;
    private static final int ANGLE_TIMES = 12;
    private static final float CANVAS_WHOLE_CENTRE_RATIO = 2.0f;
    private static final float CANVAS_WHOLE_RADIUS_RATIO = 0.6944444f;
    private static final int DEFAULT_COLOR = -10066330;
    private static final int DURATION = 1000;
    private static final float INIT_NUM = 0.0f;
    private static final float INTERPOLATOR_PARAM_1 = 0.33f;
    private static final float INTERPOLATOR_PARAM_2 = 0.0f;
    private static final float INTERPOLATOR_PARAM_3 = 0.67f;
    private static final float INTERPOLATOR_PARAM_4 = 1.0f;
    private static final int MAX_BITMAP_SIZE = 250;
    private static final int MAX_DELTA_RADIUS = 1;
    private static final int MIN_DELTA_RADIUS = 0;
    private static final float RESET_FRAME = 0.0f;
    private static final String TAG = "HwLoadingDrawable";
    private static final int TIME_DIFFERENCE = 5;
    private static final float TIME_STAMP = 60.0f;
    private static final float TIME_STAMP_1 = 0.0f;
    private static final float TIME_STAMP_2 = 10.0f;
    private static final float TIME_STAMP_3 = 33.076923f;
    private static final float TIME_STAMP_4 = 60.0f;
    private static final float TIME_STAMP_TMP = 23.076923f;
    private static final int TOTAL_ANGLE = 360;
    private static final int TOTAL_FRAMES_PER_PERIOD = 60;
    private static final float WHOLE_CENTRE_X_INIT = 0.0f;
    private static final float WHOLE_CENTRE_Y_INIT = 0.0f;
    private static final float WHOLE_DETAIL_CENTRE_RATIO = 2.0f;
    private static final float WHOLE_DETAIL_RADIUS_RATIO = 0.1f;
    private static final float WHOLE_RADIUS_INIT = 0.0f;
    private int mAlpha;
    private ObjectAnimator mAnimator;
    private int mColor;
    private double mDeltaRadius;
    private float mDetailCenterX;
    private float mDetailCenterY;
    private float mDetailRadius;
    private float mFrame;
    private float mFrameTmp;
    private Paint mPaint;
    private Interpolator mPathInterpolator;
    private float mProgress;
    private float mWholeCenterX;
    private float mWholeCenterY;
    private float mWholeRadius;

    public HwLoadingDrawableImpl(Resources resources, int i) {
        this(resources, i, DEFAULT_COLOR);
    }

    public HwLoadingDrawableImpl(Resources resources, int i, int i2) {
        int i3 = i > MAX_BITMAP_SIZE ? MAX_BITMAP_SIZE : i;
        if (i > MAX_BITMAP_SIZE) {
            i = MAX_BITMAP_SIZE;
        }
        super(resources, Bitmap.createBitmap(i3, i, Config.ARGB_8888));
        this.mProgress = 0.0f;
        this.mWholeRadius = 0.0f;
        this.mFrame = 0.0f;
        this.mFrameTmp = this.mFrame;
        this.mPathInterpolator = new CubicBezierInterpolator(INTERPOLATOR_PARAM_1, 0.0f, INTERPOLATOR_PARAM_3, INTERPOLATOR_PARAM_4);
        init(i2);
    }

    private void init(int i) {
        this.mAnimator = ObjectAnimator.ofFloat(this, "progress", new float[]{0.0f, INTERPOLATOR_PARAM_4});
        this.mAnimator.setDuration(1000);
        this.mAnimator.setRepeatCount(-1);
        this.mAnimator.setInterpolator(new LinearInterpolator());
        this.mPaint = new Paint();
        this.mWholeCenterX = 0.0f;
        this.mWholeCenterY = 0.0f;
        this.mColor = i;
        this.mPaint.setColor(this.mColor);
        setProgress(0.0f);
    }

    private void setVariousRadius(Canvas canvas) {
        this.mWholeRadius = getHalfCanvasWidthHeightMin(canvas) * CANVAS_WHOLE_RADIUS_RATIO;
        this.mDetailRadius = this.mWholeRadius * 0.1f;
        this.mDetailCenterX = this.mWholeCenterX;
        this.mDetailCenterY = this.mWholeCenterY - this.mWholeRadius;
    }

    private float getHalfCanvasWidthHeightMin(Canvas canvas) {
        this.mWholeCenterX = ((float) canvas.getWidth()) / 2.0f;
        this.mWholeCenterY = ((float) canvas.getHeight()) / 2.0f;
        return this.mWholeCenterX < this.mWholeCenterY ? this.mWholeCenterX : this.mWholeCenterY;
    }

    public void draw(Canvas canvas) {
        this.mPaint.setColor(this.mColor);
        setVariousRadius(canvas);
        if (this.mProgress * 60.0f >= 60.0f) {
            this.mProgress = 0.0f;
        }
        canvas.save();
        for (int i = 0; i < 12; i++) {
            this.mFrameTmp = (this.mProgress * 60.0f) + ((float) (i * 5));
            this.mPaint.setAlpha(((int) calculateDeltaRadiusOrAlpha(this.mFrameTmp, false)) + ALPHA_BASIC);
            canvas.drawCircle(this.mDetailCenterX, this.mDetailCenterY, this.mDetailRadius + (((float) calculateDeltaRadiusOrAlpha(this.mFrameTmp, true)) * this.mDetailRadius), this.mPaint);
            canvas.rotate(-30.0f, this.mWholeCenterX, this.mWholeCenterY);
        }
        canvas.restore();
    }

    private double calculateDeltaRadiusOrAlpha(float f, boolean z) {
        float f2 = f % 60.0f;
        this.mDeltaRadius = 0.0d;
        this.mAlpha = 128;
        if (f2 >= 0.0f && f2 < TIME_STAMP_2) {
            this.mDeltaRadius = (double) this.mPathInterpolator.getInterpolation(f2 * 0.1f);
        } else if (f2 >= TIME_STAMP_2 && f2 < TIME_STAMP_3) {
            this.mDeltaRadius = (double) this.mPathInterpolator.getInterpolation(((f2 * -0.043333333f) + INTERPOLATOR_PARAM_4) + 0.43333334f);
        } else if (f2 >= TIME_STAMP_3 && f2 < 60.0f) {
            this.mDeltaRadius = 0.0d;
        }
        if (z) {
            return this.mDeltaRadius;
        }
        this.mAlpha = (int) (this.mDeltaRadius * ((double) this.mAlpha));
        return (double) this.mAlpha;
    }

    private void setProgress(float f) {
        this.mProgress = f;
        invalidateSelf();
    }

    public void start() {
        if (this.mAnimator != null && !this.mAnimator.isRunning()) {
            this.mAnimator.start();
        }
    }

    public void stop() {
        if (this.mAnimator != null && this.mAnimator.isRunning()) {
            this.mAnimator.end();
        }
    }

    public boolean isRunning() {
        return this.mAnimator != null && this.mAnimator.isRunning();
    }

    public void setColor(int i) {
        this.mColor = i;
    }
}
